package com.sinoglobal.dumping.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.sinoglobal.dumping.R;


public abstract class Dumpling_SinoAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> implements
		Dumpling_SinoITask<Params, Progress, Result> {
	private boolean cancelable = true; // isLoadingPrompt
	private boolean isShowProgressDialog = true;
	private Context context;
	private ProgressDialog dialog;
	private final int ERROR = 4;
	private final int ERROR_DATA = 5; // 没有数据
	private final int TIME_OUT = 6; // 超时

	public Dumpling_SinoAsyncTask(Context context) {
		this.context=context;
	}
	/**
	 * @param isShowProgressDialog 是否显示加载条
	 */
	public Dumpling_SinoAsyncTask(Context context, boolean cancelable, boolean isShowProgressDialog) {
		this.context = context;
		this.isShowProgressDialog = isShowProgressDialog;
		this.cancelable = cancelable;
	}
	/**
	 *  @param isShowProgressDialog 是否显示加载条
	 */
	public Dumpling_SinoAsyncTask(Context context, boolean isShowProgressDialog) {
		this.context = context;
		this.isShowProgressDialog = isShowProgressDialog;
	}
	@Override
	protected Result doInBackground(Params... params){
		try {
			return before(params);
		} catch (Exception e) {
			e.printStackTrace();
			showUiThreadInfo(ERROR, this);
			cancel(true);
			dialog.dismiss();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Result result) {
		super.onPostExecute(result);
		dismissWaitingDialog();
		after(result);
	}
	
	private void showUiThreadInfo(final int index, final Dumpling_SinoITask task) {
		((Activity)context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                switch (index) {
                    case ERROR:
                        showShortToastMessage(context.getString(R.string.dumpling_error));
                        break;
                    case ERROR_DATA:
                        showShortToastMessage("数据解析错误");
                        break;
                    case TIME_OUT:
                        showShortToastMessage("请求超时");
                        break;
                    default:
                        break;
                }
                task.exception();
            }
        });
	}
	
	public void showShortToastMessage(String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}


	@Override
	protected void onPreExecute() {
		showWaitingDialog(cancelable);

		super.onPreExecute();
	}
	
	
	public void showWaitingDialog(boolean cancelable) {
		if(dialog==null){
			dialog = new ProgressDialog(context);
			dialog.setMessage("加载中");
		}
		dialog.setCancelable(cancelable);
		if(!dialog.isShowing() && isShowProgressDialog){
			if(!((Activity)context).isFinishing()){
				dialog.show();
			}
		}
	}
	
	public void dismissWaitingDialog() {
		if(dialog!=null && isShowProgressDialog){
			dialog.cancel();
			dialog = null;
		}
	}
}
